package com.xiaofen.license.service;

import com.xiaofen.license.constant.LicenseClientConst;
import com.xiaofen.license.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 读取许可并进行判断
 *
 * @author zhang
 * @createDate: 2021/1/22 14:26
 * @version:1.0
 * @updateUser:[修改人]
 * @updateDate:[修改时间]
 * @updateRemark:[修改备注]
 */
@Component
@Service
public class VerifyLicense {
    /**
     * 获取日志打印实例
     * */
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 许可参数
     * */
    String licenseMac = null;
    String licenseIP = null;
    String licenseDate = null;
    String licenseType = null;

    /**
     * 本地时间
     * */
    Date now = new Date();
    SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
    String localDate = date.format(now);

    /***
     * 本地MAC
     */
    String localMAC = GetMACUtils.getMacAddress();

    /**
     * 本地IP
     * */
    String localIP = GetMACUtils.getIpAddress();
    String localIP2 = GetHostIPUtils.getIP();

    /**
     * 私钥
     * */
    static String privateKey = LicenseClientConst.privateKey;

    /**
     * 项目根目录
     * */
    static String filePath = System.getProperty("user.dir");

    public boolean verify() throws Exception {
        /**读取文件*/
        //通过绝对路径获取文件
        String license = FileUtils.readTxt(new File(filePath + File.separator + "license.txt"));
        logger.info("读取密钥为： " + license);
        try {
            if (license == null) {
                return false;
            }
            //解密
            byte[] deBaseData = Base64Utils.decode(license);
            byte[] decodedData = RSAUtils.decryptByPrivateKey(deBaseData, privateKey);
            String target = new String(decodedData);
            logger.info("解密后:  " + target);
            logger.info("本地为： " + localMAC + "|" + localIP + "|" + localDate);

            //截取字段
            String[] licData = target.split("\\|");
            licenseMac = licData[0];
            licenseIP = licData[1];
            licenseDate = licData[2];
            licenseType = licData[3];

            /**判断是否为日期格式*/
            if (!isValidDate(licenseDate)) {
                return false;
            }
            /**判断现在的时间比许可时间大*/
            if (Integer.parseInt(localDate) > Integer.parseInt(licenseDate)) {
                return false;
            }

            /**判断IP地址是否一致*/
            if (!licenseIP.equals(localIP)){
                return false;
            }

            /**判断MAC地址是否一致*/
            if (!licenseMac.equals(localMAC)){
                return false;
            }

        }catch (Exception e){
            return false;
        }
        if ("0".equals(licenseType)){
            licenseType = "Production";
        }else if ("1".equals(licenseType)){
            licenseType = "Develop";
        }else if ("2".equals(licenseType)){
            licenseType = "Test";
        }else if ("3".equals(licenseType)){
            licenseType = "Trial";
        }
        logger.info(licenseType + " license, expires at " + licenseDate + "!");
        return true;

    }

    /**
     * Description 判断是否为日期格式
     * @param str
     * @return
     */
    public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return convertSuccess;
    }

    public static void main(String[] args) throws Exception{
        VerifyLicense verifyLicense = new VerifyLicense();
        verifyLicense.testIp();
    }

    public void testIp(){
        System.out.println("============IP " +  localIP);
        System.out.println("============IP2 " +  localIP2);
        System.out.println("============MAC " +  localMAC);

    }
}
